package com.op.teachingliteapp.fragment.resourcedetails;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.android_file_manger.MainActivity;
import com.op.teachingliteapp.R;
import com.op.teachingliteapp.bean.FileBean;
import com.op.teachingliteapp.common.Common;
import com.op.teachingliteapp.fragment.viewer.ViewPagerFragment;
import com.op.teachingliteapp.util.DirUtils;
import com.op.teachingliteapp.util.ScreenSendUtils;
import com.op.teachingliteapp.util.SharedPreferencesUtils;
import org.vudroid.pdfdroid.codec.PdfContext;

/*
 * //这个fragment写选择列表
 */
public class ResourceDetailsFragment extends Fragment {
    private static final String sdcardPath = Environment
            .getExternalStorageDirectory().toString();
    public static String Location = sdcardPath + "/DEMO/";
    private View root;
    private static final String userDir = null;

    @SuppressWarnings("unused")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        MainActivity.fileName = null;//清空文件管理器选中的文件
        Log.i("PPT列表", sdcardPath);
       String courseName= getArguments().getString("courseName");
        View root = inflater.inflate(R.layout.fragment_resource_details_ppt,
                container, false);
        ListView mListView = (ListView) root.findViewById(R.id.pdf_listView);
        ArrayList<ResourceDetailsDataProvider> ppt_list = new ArrayList<ResourceDetailsDataProvider>();
        String userid = SharedPreferencesUtils.get(Common.Prefs.USER_ID, getActivity());
        DirUtils dirUtils = new DirUtils(getActivity(), userid);
        List<FileBean> fileBeanList = dirUtils.getFileList(dirUtils.getCoursePptDir(courseName));
        for (FileBean fileBean:fileBeanList) {
            ResourceDetailsDataProvider resourceDetailsDataProvider = new ResourceDetailsDataProvider(fileBean.getName(), fileBean.getPath(), R.drawable.pdf);
            ppt_list.add(resourceDetailsDataProvider);
        }

        final ResourceDetailsAdapter mAdapter = new ResourceDetailsAdapter(
                getActivity(), ppt_list);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                ResourceDetailsDataProvider resourceDetailsDataProvider = mAdapter
                        .getItem(position);

                String url = resourceDetailsDataProvider.getUrl();
                Log.i("ResourceDetailsFragment", url);
                if (isPdfOpenable(url)) {
                    ViewPagerFragment pptShow = new ViewPagerFragment();// 这个是打开pdf速度快，没有画笔的
                    Bundle bundle = new Bundle();
                    bundle.putString("url", url);
                    pptShow.setArguments(bundle);

                    getFragmentManager().beginTransaction()
                            .addToBackStack(null)
                            .replace(R.id.fragment_replace_two, pptShow).commit();
                } else {
                    Toast.makeText(getActivity(), "该文件无法打开", Toast.LENGTH_SHORT).show();
                }


            }

        });

        Button add_ppt_btn = (Button) root.findViewById(R.id.ppt_add);
        add_ppt_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });
        this.root = root;
        return root;
    }

    @Override
    public void onResume() {
        // 用于在文件管理器里打开pdf后fragment显示
        if (MainActivity.fileName != null
                && MainActivity.fileName.endsWith(".pdf")) {
            ViewPagerFragment pptShow = new ViewPagerFragment();

            Bundle bundle = new Bundle();
            bundle.putString("url", MainActivity.filePath);
            pptShow.setArguments(bundle);

            getFragmentManager().beginTransaction().addToBackStack(null)
                    .replace(R.id.fragment_replace_two, pptShow).commit();
            MainActivity.fileName = null;
        } else {
            ScreenSendUtils.sendWithDely(root);
        }

        super.onResume();
    }

    private boolean isPdfOpenable(String url) {

        PdfContext pdfContext = new PdfContext();
        try {
            pdfContext.openDocument(url);
        } catch (Exception e) {
            return false;
        }


        return true;
    }
}
